<!-- 设备管理-设备列表页面 -->
<template>
  <div class="app-container" style="margin: 20px">
    <px-form v-show="showSearch" ref="queryRef" class="custom-form-ui" :model="queryParams" :inline="true" label-width="90px">
      <px-form-item label="时间" style="width: 450px !important">
        <px-date-picker
          v-model="dateRange"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          :default-time="defaultTime"
        />
      </px-form-item>
      <px-button style="margin-top: -17px" type="primary" icon="" @click="handleQuery('search')">搜索</px-button>
      <px-button style="margin-top: -17px" icon="" @click="resetQuery">重置</px-button>
    </px-form>

    <px-row :gutter="10" class="mb8">
      <px-col :span="1.5">
        <px-button v-has="'EXCEPTION_HANDLING_ADD'" type="primary" icon="" @click="handleAdd">新增</px-button>
      </px-col>
      <!-- <px-col :span="1.5">
        <px-button type="primary" plain icon="" @click="handleExport">导出报表</px-button>
      </px-col> -->

      <!-- <px-col :span="1.5">
        <px-button v-has="'CommonEquipListDelete'" type="danger" plain icon="" :disabled="multiple" @click="handleDelete">删除</px-button>
      </px-col> -->
    </px-row>

    <px-table
      v-if="!equipopen && !equipopenDetail"
      v-loading="loading"
      :data="typeList"
      height="calc(100vh - 369px)"
      :show-overflow-tooltip="false"
      border
      @selection-change="handleSelectionChange"
    >
      <px-table-column label="序号" width="60" align="center" fixed="left">
        <template #default="{ $index }">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </px-table-column>
      <px-table-column width="200" label="类型" align="center" prop="category" />
      <px-table-column label="异常概述" prop="eventDescription">
        <template #default="scope">
          <div style="word-break: break-all; white-space: normal">{{ scope.row.eventDescription }}</div>
        </template>
      </px-table-column>

      <px-table-column label="异常发生时间" prop="eventTimeStart">
        <template #default="scope">
          <span>{{ scope.row.eventTimeStart }}-{{ scope.row.eventTimeEnd }}</span>
        </template>
      </px-table-column>

      <px-table-column label="发现地点" width="160" prop="findPlace">
        <template #default="scope">
          <span>{{ scope.row.findPlace }}</span>
        </template>
      </px-table-column>
      <px-table-column fixed="right" label="操作" align="center" width="180">
        <template #default="scope">
          <px-button v-has="'EXCEPTION_HANDLING_DETAIL'" link type="primary" @click="handleDetail(scope.row.id)">
            <px-tooltip class="box-item" content="详情" placement="top">
              <px-icon><Tickets /></px-icon> </px-tooltip
          ></px-button>
          <px-button v-has="'EXCEPTION_HANDLING_EDITOR'" link type="primary" @click="handleEditor(scope.row.id)">
            <px-tooltip class="box-item" content="编辑" placement="top">
              <px-icon><EditPen /></px-icon> </px-tooltip
          ></px-button>
          <px-button v-has="'EXCEPTION_HANDLING_EXPORT'" link type="primary" @click="handleExport(scope.row)">
            <px-tooltip class="box-item" content="导出" placement="top">
              <px-icon><Bottom /></px-icon> </px-tooltip
          ></px-button>
          <px-button v-has="'EXCEPTION_HANDLING_DELETE'" link type="danger" @click="handleDelete(scope.row)"
            ><px-tooltip class="box-item" content="删除" placement="top">
              <px-icon><Delete /></px-icon> </px-tooltip
          ></px-button>
        </template>
      </px-table-column>
      <template #empty>
        <px-empty :image-size="150" />
      </template>
    </px-table>

    <pagination
      v-if="!equipopen && !equipopenDetail"
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      style="margin-bottom: -10px"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->

    <px-scrollbar v-show="equipopenDetail" class="addEquip">
      <px-button style="margin-left: 24px" type="primary" :icon="ArrowLeft" @click="equipopenDetailcancel">返回</px-button>
      <px-form
        ref="detailruleFormRef"
        :disabled="fromdisable"
        :rules="rules"
        :model="equipdata"
        :inline="true"
        class="demo-form-inline"
        label-width="160px"
      >
        <div
          class="box-card1"
          :style="{
            height: jibenxinxiFlg == true ? '' : '42px'
          }"
        >
          <div class="card-header" @click="setqingdanFlg()">
            <div class="card-title">异常信息</div>
            <div class="card-main">
              <px-icon v-if="!jibenxinxiFlg"><ArrowDown /></px-icon>
              <px-icon v-else><ArrowUp /></px-icon>
            </div>
          </div>
          <div class="form-div">
            <px-row>
              <px-col :span="24">
                <px-form-item label="类型" prop="category">
                  <px-select v-model="equipdata.category" filterable placeholder="请选择类型" clearable>
                    <px-option v-for="(item, index) in materialOptions" :key="index" :label="item.itemName" :value="item.itemName" />
                  </px-select>
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="24">
                <px-form-item label="异常概述" prop="eventDescription">
                  <px-input
                    v-model="equipdata.eventDescription"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入异常概述"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="8">
                <px-form-item label="异常发生时间" prop="eqdateRange">
                  <px-date-picker
                    v-model="equipdata.eqdateRange"
                    style="width: calc(100% - 100px)"
                    type="datetimerange"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    format="YYYY-MM-DD HH:mm"
                    value-format="YYYY-MM-DD HH:mm"
                    :default-time="defaultTime"
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="24">
                <px-form-item label="异常设备及影响范围 (厂务部)" prop="effectFactory">
                  <px-input
                    v-model="equipdata.effectFactory"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="24">
                <px-form-item label="异常设备及影响范围 (制造部)" prop="effectProduce">
                  <px-input
                    v-model="equipdata.effectProduce"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row style="position: relative">
              <px-col :span="8">
                <px-form-item label="异常状况及发现人" prop="findUserId">
                  <px-select
                    v-model="equipdata.findUserId"
                    placeholder="请选择"
                    style="width: calc(100% - 100px)"
                    clearable
                    filterable
                    @change="changesfindUserId"
                  >
                    <px-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value" />
                  </px-select>
                </px-form-item>
              </px-col>
              <px-col :span="8">
                <px-form-item label="发现时间" prop="findTime">
                  <px-date-picker
                    v-model="equipdata.findTime"
                    style="width: calc(100% - 100px)"
                    placeholder="请选择发现时间"
                    type="datetime"
                    format="YYYY-MM-DD HH:mm"
                    value-format="YYYY-MM-DD HH:mm"
                  />
                </px-form-item>
              </px-col>

              <px-col v-show="!findPlaceflg" :span="8">
                <px-form-item label="发现地点" prop="findPlace">
                  <div style="width: calc(100% - 100px)">
                    <px-tree-select
                      v-model="equipdata.findPlace"
                      clearable
                      filterable
                      style="width: 100%"
                      placeholder="请选择"
                      :props="defaultProps"
                      :data="positionList"
                      check-strictly
                      :render-after-expand="false"
                      @node-click="nodeselect2"
                    />
                  </div>
                </px-form-item>
              </px-col>
              <px-col v-show="findPlaceflg" :span="8">
                <px-form-item label="发现地点" prop="findPlace">
                  <div style="width: calc(100% - 100px)">
                    <px-input v-model="equipdata.findPlace" style="width: 100%" placeholder="请输入" clearable />
                  </div>
                </px-form-item>
              </px-col>
              <px-tooltip class="box-item" effect="dark" content="是否设置为自定义地点" placement="top-end">
                <px-switch v-model="findPlaceflg" style="position: absolute; right: 44px" />
              </px-tooltip>
            </px-row>
            <px-row>
              <px-col :span="8">
                <px-form-item label="异常状况及处理人" prop="dealUserId">
                  <px-select
                    v-model="equipdata.dealUserId"
                    placeholder="请选择"
                    style="width: calc(100% - 100px)"
                    clearable
                    filterable
                    @change="changesdealUserId"
                  >
                    <px-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value" />
                  </px-select>
                </px-form-item>
              </px-col>
              <px-col :span="8">
                <px-form-item label="处理时间" prop="dealTime">
                  <px-date-picker
                    v-model="equipdata.dealTime"
                    style="width: calc(100% - 100px)"
                    placeholder="请选择处理时间"
                    type="datetime"
                    format="YYYY-MM-DD HH:mm"
                    value-format="YYYY-MM-DD HH:mm"
                  />
                </px-form-item>
              </px-col>
              <px-col v-show="!dealPlaceCodeflg" :span="8">
                <px-form-item label="处理地点" prop="dealPlace">
                  <div style="width: calc(100% - 100px)">
                    <px-tree-select
                      v-model="equipdata.dealPlace"
                      clearable
                      style="width: 100%"
                      placeholder="请选择"
                      :props="defaultProps"
                      :data="positionList"
                      check-strictly
                      :render-after-expand="false"
                      @node-click="nodeselect"
                    />
                  </div>
                </px-form-item>
              </px-col>
              <px-col v-show="dealPlaceCodeflg" :span="8">
                <px-form-item label="处理地点" prop="dealPlace">
                  <div style="width: calc(100% - 100px)">
                    <px-input v-model="equipdata.dealPlace" style="width: 100%" placeholder="请输入" clearable />
                  </div>
                </px-form-item>
              </px-col>
              <px-tooltip class="box-item" effect="dark" content="是否设置为自定义地点" placement="top-end">
                <px-switch v-model="dealPlaceCodeflg" style="position: absolute; right: 44px" />
              </px-tooltip>
            </px-row>
          </div>
        </div>
        <div
          class="box-card2"
          :style="{
            height: jiqicanshuFlg == true ? '' : '42px'
          }"
        >
          <div class="card-header" @click="setjiqicanshuFlg()">
            <div class="card-title">处理详情</div>
            <div class="card-main">
              <px-icon v-if="!jiqicanshuFlg"><ArrowDown /></px-icon>
              <px-icon v-else><ArrowUp /></px-icon>
            </div>
          </div>
          <div class="form-div">
            <px-row>
              <px-col :span="24">
                <px-form-item label="处理过程" prop="dealProcess">
                  <px-input
                    v-model="equipdata.dealProcess"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="24">
                <px-form-item label="原因分析" prop="reasonAnalysis">
                  <px-input
                    v-model="equipdata.reasonAnalysis"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="24">
                <px-form-item label="预防措施" prop="preventMeasure">
                  <px-input
                    v-model="equipdata.preventMeasure"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="8">
                <px-form-item label="填表人" prop="fillUserId">
                  <px-select
                    v-model="equipdata.fillUserId"
                    placeholder="请选择"
                    style="width: calc(100% - 100px)"
                    clearable
                    filterable
                    @change="changesfillUserId"
                  >
                    <px-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value" />
                  </px-select>
                </px-form-item>
              </px-col>
              <px-col :span="8">
                <px-form-item label="科经理" prop="managerUserId">
                  <px-select
                    v-model="equipdata.managerUserId"
                    placeholder="请选择"
                    style="width: calc(100% - 100px)"
                    clearable
                    filterable
                    @change="changesmanagerUserId"
                  >
                    <px-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value" />
                  </px-select>
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="8">
                <px-form-item label="系统工程师" prop="engineerUserId">
                  <px-select
                    v-model="equipdata.engineerUserId"
                    placeholder="请选择"
                    style="width: calc(100% - 100px)"
                    clearable
                    filterable
                    @change="changesengineerUserId"
                  >
                    <px-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value" />
                  </px-select>
                </px-form-item>
              </px-col>
              <px-col :span="8">
                <px-form-item label="部门经理" prop="deptManagerUserId">
                  <px-select
                    v-model="equipdata.deptManagerUserId"
                    placeholder="请选择"
                    style="width: calc(100% - 100px)"
                    clearable
                    filterable
                    @change="changesdeptManagerUserId"
                  >
                    <px-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value" />
                  </px-select>
                </px-form-item>
              </px-col>
            </px-row>
            <px-row>
              <px-col :span="24">
                <px-form-item label="备注">
                  <px-input
                    v-model="equipdata.remark"
                    style="width: calc(100% - 100px)"
                    :autosize="{ minRows: 2, maxRows: 3 }"
                    type="textarea"
                    maxlength="1000"
                    show-word-limit
                    placeholder="请输入"
                    clearable
                  />
                </px-form-item>
              </px-col>
            </px-row>
          </div>
        </div>
        <div
          class="box-card2"
          :style="{
            height: jiqicanshuFlg == true ? '' : '42px'
          }"
        >
          <div class="card-header" @click="setjiqicanshuFlg()">
            <div class="card-title">图片</div>
            <div class="card-main">
              <px-icon v-if="!jiqicanshuFlg">
                <ArrowDown />
              </px-icon>
              <px-icon v-else>
                <ArrowUp />
              </px-icon>
            </div>
          </div>
          <px-scrollbar class="form-div">
            <div class="info-form-content">
              <PhotoUpload
                v-if="buttonTitle != '详情'"
                :key="'ptoto1'"
                photoType="add"
                :photoData="{ moduleName: 'OPERATION_WATER' }"
                :photoList="equipdata.photo"
                @changePhotoList="changePhotoList"
                @savePhotoStatus="savePhotoStatus"
              />
              <PhotoUpload v-else photoType="view" :photoData="{ moduleName: 'OPERATION_WATER' }" :photoList="equipdata.photo" />
            </div>
          </px-scrollbar>
        </div>
      </px-form>
      <div
        class="box-card3"
        :style="{
          height: shebeitaizhangFlg == true ? '' : '42px'
        }"
      >
        <div class="card-header" @click="setshebeitaizhangFlg()">
          <div class="card-title">关联文档</div>
          <div class="card-main">
            <px-icon v-if="!shebeitaizhangFlg">
              <ArrowDown />
            </px-icon>
            <px-icon v-else>
              <ArrowUp />
            </px-icon>
          </div>
        </div>
        <px-scrollbar class="form-div">
          <div class="info-form-content">
            <FileUpload
              v-if="buttonTitle != '详情'"
              fileType="add"
              :fileData="{ moduleName: 'OPERATION_WATER' }"
              :fileList="equipdata.attachment"
              @changeFileList="changeFileList"
              @saveFileStatus="saveFileStatus"
            />
            <FileUpload v-else fileType="view" :fileData="{ moduleName: 'OPERATION_WATER' }" :fileList="equipdata.attachment" />
          </div>
        </px-scrollbar>
      </div>
    </px-scrollbar>
    <div v-if="equipopenDetail && !fromdisable" v-loading="subLoading" class="px-dialog-footer">
      <px-button @click="equipopenDetailcancel">取消</px-button>
      <px-button v-if="fromdisable" type="primary" @click="equipopenDetailcancel">关闭</px-button>
      <px-button v-else type="primary" :disabled="photoStatusFlag || fileStatusFlag" @click="equipopenDetailsubmitForm(detailruleFormRef)"
        >保存</px-button
      >
    </div>
    <px-dialog v-model="progressVisible" draggable clickHideContent title="上传进度" width="30%" center align-center>
      <px-progress :percentage="Math.floor(progressPercent)" />
    </px-dialog>
  </div>
</template>

<script setup name="Dict" lang="ts">
// import useDictStore from '@/store/modules/dict'
defineOptions({
  name: "SystemDataDict"
});
// import { router } from "@/router";
import { ArrowLeft } from "@px-ui/icons-vue";
import { useRoute } from "vue-router";
import { PxMessage, PxMessageBox } from "px-ui";
import { getCurrentInstance, ref, toRefs, reactive, onMounted } from "vue";
import { getToken } from "@/utils/auth";
import PhotoUpload from "@/components/Upload/PhotoUpload.vue";
import FileUpload from "@/components/Upload/FileUpload.vue";
import { userlistPage } from "@/api/waterQualityDetection";
import {
  handoverExceptionHandlequeryListPage,
  handoverExceptionHandleadd,
  handoverExceptionHandledelete,
  handoverExceptionHandleDetail,
  handoverExceptionHandleupdate
} from "@/api/waterClass";
import { getTableData } from "@/api/system/positionManage";
import { queryFacPositionTree } from "@/api/visual/visualConfig";
import { queryChildByTypeCodeAndItemValue } from "@/api/waterClass";
const jibenxinxiFlg = ref(true);
const defaultTime: [Date, Date] = [new Date(2000, 1, 1, 0, 0, 1), new Date(2000, 1, 1, 23, 59, 59)];
const setqingdanFlg = () => {
  jibenxinxiFlg.value = !jibenxinxiFlg.value;
};
const defaultProps = {
  children: "children",
  label: "name",
  value: "name"
};
const jiqicanshuFlg = ref(true);
const setjiqicanshuFlg = () => {
  jiqicanshuFlg.value = !jiqicanshuFlg.value;
};
const shebeitaizhangFlg = ref(true);
const setshebeitaizhangFlg = () => {
  shebeitaizhangFlg.value = !shebeitaizhangFlg.value;
};
const moxingshezhiFlg = ref(true);
const setmoxingshezhiFlg = () => {
  moxingshezhiFlg.value = !moxingshezhiFlg.value;
};
const nodeselect = data => {
  console.log(data);
  equipdata.value.dealPlace = data.name;
};
// 新的图片列表
const changePhotoList = val => {
  equipdata.value.photo = val;
  console.log(val);
};
// 图片是否上传完，是否可以保存
const savePhotoStatus = val => {
  photoStatusFlag.value = val;
};
// 新的文件列表
const changeFileList = val => {
  equipdata.value.attachment = val;
  console.log(val);
};
// 文件是否上传完，是否可以保存
const saveFileStatus = val => {
  fileStatusFlag.value = val;
};
const nodeselect2 = data => {
  console.log(data);
  equipdata.value.findPlaceCode = data.code;
  setTimeout(() => {
    equipdata.value.findPlace = data.name;
  }, 100);
};
const changesfindUserId = val => {
  userList.value.forEach(item => {
    if (item.value == val) {
      console.log(item);
      equipdata.value.findUserName = item.label;
    }
  });
};

const changesdealUserId = val => {
  userList.value.forEach(item => {
    if (item.value == val) {
      console.log(item);
      equipdata.value.dealUserName = item.label;
    }
  });
};

const changesfillUserId = val => {
  userList.value.forEach(item => {
    if (item.value == val) {
      console.log(item);
      equipdata.value.fillUserName = item.label;
    }
  });
};

const changesmanagerUserId = val => {
  userList.value.forEach(item => {
    if (item.value == val) {
      console.log(item);
      equipdata.value.managerUserName = item.label;
    }
  });
};

const changesengineerUserId = val => {
  userList.value.forEach(item => {
    if (item.value == val) {
      console.log(item);
      equipdata.value.engineerUserName = item.label;
    }
  });
};

const changesdeptManagerUserId = val => {
  userList.value.forEach(item => {
    if (item.value == val) {
      console.log(item);
      equipdata.value.deptManagerUserName = item.label;
    }
  });
};

onMounted(() => {
  getList();

  getqueryCategoryList();
});

const typeList = ref([]);
const detailruleFormRef = ref();
const progressVisible = ref(false);
const equipopen = ref(false);
const equipopenDetail = ref(false);
const fromdisable = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const progressPercent = ref(0);
const dateRange = ref([]);

const buttonTitle = ref("");
const data = reactive({
  liquidList: [],
  transportCompanyList: [],
  receiveCompanyList: [],
  typeIdList: [],
  categoryList: [],
  equipmentTypeList: [],
  positionList: [],
  equipmentDeptList: [],
  modelIdList: [],
  form: {},
  photoStatusFlag: false,
  fileStatusFlag: false,
  equipdata: {
    id: "",
    eventTimeStart: "",
    eventTimeEnd: "",
    eventDescription: "",
    effectFactory: "",
    effectProduce: "",
    findUserId: "",
    findUserName: "",
    findTime: "",
    findPlaceCode: "",
    findPlace: "",
    dealUserId: "",
    dealUserName: "",
    dealTime: "",
    dealPlaceCode: "",
    dealPlace: "",
    dealProcess: "",
    reasonAnalysis: "",
    preventMeasure: "",
    fillUserId: "",
    fillUserName: "",
    managerUserId: "",
    managerUserName: "",
    engineerUserId: "",
    engineerUserName: "",
    deptManagerUserId: "",
    deptManagerUserName: "",
    remark: "",
    eqdateRange: [],
    photo: [],
    attachment: [],
    category: ""
  },
  queryParams: {
    orderByColumn: "",
    isAsc: "",
    eventDateStart: "",
    eventDateEnd: "",
    orderBy: "",
    pageNum: 1,
    pageSize: 20
  },
  rules: {
    category: [{ required: true, message: "类型不能为空", trigger: "change" }],
    eventDescription: [{ required: true, message: "异常概述不能为空", trigger: "blur" }],
    eqdateRange: [{ required: true, message: "异常发生时间不能为空", trigger: "blur" }],
    effectFactory: [{ required: true, message: "异常设备及影响范围 (厂务部)不能为空", trigger: "blur" }],
    effectProduce: [{ required: true, message: "异常设备及影响范围 (制造部)不能为空", trigger: "blur" }],
    findUserId: [{ required: true, message: "异常状况及发现人不能为空", trigger: "change" }],
    findTime: [{ required: true, message: "发现时间不能为空", trigger: "blur" }],
    findPlaceCode: [{ required: true, message: "发现地点不能为空", trigger: "change" }],
    findPlace: [{ required: true, message: "发现地点不能为空", trigger: "blur" }],
    dealUserId: [{ required: true, message: "异常状况及处理人不能为空", trigger: "change" }],
    dealTime: [{ required: true, message: "处理时间不能为空", trigger: "blur" }],
    dealPlaceCode: [{ required: true, message: "处理地点不能为空", trigger: "change" }],
    dealPlace: [{ required: true, message: "处理地点不能为空", trigger: "blur" }],
    dealProcess: [{ required: true, message: "处理过程不能为空", trigger: "blur" }],
    reasonAnalysis: [{ required: true, message: "原因分析不能为空", trigger: "blur" }],
    preventMeasure: [{ required: true, message: "预防措施不能为空", trigger: "blur" }],
    fillUserId: [{ required: true, message: "填表人不能为空", trigger: "change" }],
    managerUserId: [{ required: true, message: "科经理不能为空", trigger: "change" }],
    engineerUserId: [{ required: true, message: "系统工程师不能为空", trigger: "change" }],
    deptManagerUserId: [{ required: true, message: "部门经理不能为空", trigger: "change" }],
    remark: [{ required: true, message: "备注不能为空", trigger: "blur" }]
  },
  headers: {
    Authorization: "Bearer " + getToken().accessToken || ""
  },
  moduleName: "",
  dialogImageUrl: "",
  imageVisible: false,
  flowTypeList: [],
  transportTypeList: [],
  userList: [],
  positionTree: []
});

const { positionTree, userList, queryParams, rules, equipdata, photoStatusFlag, fileStatusFlag } = toRefs(data);
const findPlaceflg = ref(false);
const dealPlaceCodeflg = ref(false);
const checkTime = () => {
  if (equipdata.value.manufactureDate && equipdata.value.warrantyEndDate) {
    if (equipdata.value.manufactureDate > equipdata.value.warrantyEndDate) {
      PxMessage({
        message: "开始时间不能大于结束时间",
        type: "error"
      });
    }
  }
};
const materialOptions = ref([]);
const positionList = ref([]);
const getqueryCategoryList = () => {
  userlistPage({ page: 1, pageSize: 9999, deptNameList: ["WATER"], status: 0 }).then((response: any) => {
    userList.value = response.data.map(item => ({ label: `${item.username}-${item.realName}`, value: item.userId }));
    console.log(response);
  });
  // queryFacPositionTree({ facCode: "FAC_83abf9" }).then(res => {
  //   console.log(res);
  //   positionTree.value = res;
  // });
  getTableData({}).then(res => {
    positionList.value = res;
  });
  queryChildByTypeCodeAndItemValue({
    typeCode: "WTTS_EXCEPTION_HANDLE_CATEGORY"
  }).then((response: any) => {
    //console.log(response);
    materialOptions.value = response;
  });
};

function getList() {
  loading.value = true;
  if (dateRange.value != null) {
    queryParams.value.eventDateStart = dateRange.value[0];
    queryParams.value.eventDateEnd = dateRange.value[1];
  } else {
    queryParams.value.eventDateStart = "";
    queryParams.value.eventDateEnd = "";
  }

  handoverExceptionHandlequeryListPage(queryParams.value).then((response: any) => {
    typeList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */

function equipopenDetailcancel() {
  equipopenDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery(type) {
  if (type == "search") {
    queryParams.value.pageNum = 1;
  }
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    dictWasteCode: "",
    transportCompanyName: "",
    receiveCompanyName: "",
    status: "VALID"
  };
  dateRange.value = [];
  handleQuery("reset");
}

/** 新增按钮操作 */
function handleAdd() {
  buttonTitle.value = "新增";
  jibenxinxiFlg.value = true;
  jiqicanshuFlg.value = true;
  shebeitaizhangFlg.value = true;
  moxingshezhiFlg.value = true;
  equipopenDetail.value = true;
  fromdisable.value = false;
  equipdata.value = {
    id: "",
    eventTimeStart: "",
    eventTimeEnd: "",
    eventDescription: "",
    effectFactory: "",
    effectProduce: "",
    findUserId: "",
    findUserName: "",
    findTime: "",
    findPlaceCode: "",
    findPlace: "",
    dealUserId: "",
    dealUserName: "",
    dealTime: "",
    dealPlaceCode: "",
    dealPlace: "",
    dealProcess: "",
    reasonAnalysis: "",
    preventMeasure: "",
    fillUserId: "",
    fillUserName: "",
    managerUserId: "",
    managerUserName: "",
    engineerUserId: "",
    engineerUserName: "",
    deptManagerUserId: "",
    deptManagerUserName: "",
    remark: "",
    eqdateRange: [],
    photo: [],
    attachment: [],
    category: ""
  };
  equipdata.value.fillUserId = JSON.parse(localStorage.getItem("userInfo")).userId;
  equipdata.value.fillUserName = `${JSON.parse(localStorage.getItem("userInfo")).username}-${JSON.parse(localStorage.getItem("userInfo")).realName}`;
}
const handleDetail = async val => {
  buttonTitle.value = "详情";
  jibenxinxiFlg.value = true;
  jiqicanshuFlg.value = true;
  shebeitaizhangFlg.value = true;
  moxingshezhiFlg.value = true;
  const data = await handoverExceptionHandleDetail({ id: val });
  //console.log(data);

  equipopenDetail.value = true;
  // equipopenDetail.value = true;
  fromdisable.value = true;
  equipdata.value = data;
  equipdata.value.findUserId = data.findUserId != "" ? Number(data.findUserId) : "";
  equipdata.value.dealUserId = data.dealUserId != "" ? Number(data.dealUserId) : "";
  equipdata.value.fillUserId = data.fillUserId != "" ? Number(data.fillUserId) : "";
  equipdata.value.managerUserId = data.managerUserId != "" ? Number(data.managerUserId) : "";
  equipdata.value.engineerUserId = data.engineerUserId != "" ? Number(data.engineerUserId) : "";
  equipdata.value.deptManagerUserId = data.deptManagerUserId != "" ? Number(data.deptManagerUserId) : "";
  equipdata.value.eqdateRange = [data.eventTimeStart, data.eventTimeEnd];
  if (data?.photo && data?.photo.length > 0) {
    data?.photo.forEach(item => {
      item.url = item.fileUrl;
    });
    equipdata.value.photo = data?.photo;
  } else {
    equipdata.value.photo = [];
  }
  if (data.attachment && data.attachment.length > 0) {
    data.attachment.forEach(item => {
      item.url = item.fileUrl;
      item.name = item.originalName;
    });
    equipdata.value.attachment = data.attachment;
  } else {
    equipdata.value.attachment = [];
  }
  //console.log(equipdata);
};

const handleEditor = async val => {
  buttonTitle.value = "编辑";
  jibenxinxiFlg.value = true;
  jiqicanshuFlg.value = true;
  shebeitaizhangFlg.value = true;
  moxingshezhiFlg.value = true;

  const data = await handoverExceptionHandleDetail({ id: val });
  //console.log(data);
  equipopenDetail.value = true;
  fromdisable.value = false;
  equipdata.value = data;
  equipdata.value.findUserId = data.findUserId != "" ? Number(data.findUserId) : "";
  equipdata.value.dealUserId = data.dealUserId != "" ? Number(data.dealUserId) : "";
  equipdata.value.fillUserId = data.fillUserId != "" ? Number(data.fillUserId) : "";
  equipdata.value.managerUserId = data.managerUserId != "" ? Number(data.managerUserId) : "";
  equipdata.value.engineerUserId = data.engineerUserId != "" ? Number(data.engineerUserId) : "";
  equipdata.value.deptManagerUserId = data.deptManagerUserId != "" ? Number(data.deptManagerUserId) : "";
  equipdata.value.eqdateRange = [data.eventTimeStart, data.eventTimeEnd];
  if (data?.photo && data?.photo.length > 0) {
    data?.photo.forEach(item => {
      item.url = item.fileUrl;
    });
    equipdata.value.photo = data?.photo;
  } else {
    equipdata.value.photo = [];
  }
  if (data.attachment && data.attachment.length > 0) {
    data.attachment.forEach(item => {
      item.url = item.fileUrl;
      item.name = item.originalName;
    });
    equipdata.value.attachment = data.attachment;
  } else {
    equipdata.value.attachment = [];
  }
};
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 提交按钮 */
const subLoading = ref(false);
const equipopenDetailsubmitForm = async (formEl: any) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      subLoading.value = true;
      const params = {
        id: equipdata.value.id,
        eventTimeStart: equipdata.value.eqdateRange[0],
        eventTimeEnd: equipdata.value.eqdateRange[1],
        eventDescription: equipdata.value.eventDescription,
        effectFactory: equipdata.value.effectFactory,
        effectProduce: equipdata.value.effectProduce,
        findUserId: equipdata.value.findUserId,
        findUserName: equipdata.value.findUserName,
        findTime: equipdata.value.findTime,
        findPlaceCode: findPlaceflg.value ? "" : equipdata.value.findPlaceCode,
        findPlace: equipdata.value.findPlace,
        dealUserId: equipdata.value.dealUserId,
        dealUserName: equipdata.value.dealUserName,
        dealTime: equipdata.value.dealTime,
        dealPlaceCode: equipdata.value.dealPlaceCode,
        dealPlace: equipdata.value.dealPlace,
        dealProcess: equipdata.value.dealProcess,
        reasonAnalysis: equipdata.value.reasonAnalysis,
        preventMeasure: equipdata.value.preventMeasure,
        fillUserId: equipdata.value.fillUserId,
        fillUserName: equipdata.value.fillUserName,
        managerUserId: equipdata.value.managerUserId,
        managerUserName: equipdata.value.managerUserName,
        engineerUserId: equipdata.value.engineerUserId,
        engineerUserName: equipdata.value.engineerUserName,
        deptManagerUserId: equipdata.value.deptManagerUserId,
        deptManagerUserName: equipdata.value.deptManagerUserName,
        remark: equipdata.value.remark,
        updateTime: equipdata.value.updateTime,
        photo: equipdata.value.photo,
        attachment: equipdata.value.attachment,
        category: equipdata.value.category
      };
      if (buttonTitle.value == "新增") {
        console.log(params);
        if (params.photo) {
          if (params.photo.length > 0) {
            params.photo = params.photo.map(item => (item?.response?.data ? item?.response?.data.id : item.id));
          }
        }
        if (params.attachment) {
          if (params.attachment.length > 0) {
            params.attachment = params.attachment.map(item => (item?.response?.data ? item?.response?.data.id : item.id));
          }
        }
        handoverExceptionHandleadd(params)
          .then((response: any) => {
            PxMessage({
              message: `${buttonTitle.value == "新增" ? "添加成功" : "修改成功"}`,
              type: "success"
            });
            getList();
            subLoading.value = false;
            equipopenDetail.value = false;
          })
          .catch(() => {
            subLoading.value = false;
          });
      } else {
        if (params.photo.length > 0) {
          params.photo = params.photo.map(item => (item?.response?.data ? item?.response?.data.id : item.id));
        }
        if (params.attachment.length > 0) {
          params.attachment = params.attachment.map(item => (item?.response?.data ? item?.response?.data.id : item.id));
        }
        handoverExceptionHandleupdate(params)
          .then((response: any) => {
            PxMessage({
              message: `${buttonTitle.value == "新增" ? "添加成功" : "修改成功"}`,
              type: "success"
            });
            getList();
            subLoading.value = false;
            equipopenDetail.value = false;
          })
          .catch(() => {
            subLoading.value = false;
          });
      }
    }
  });
};
/** 删除按钮操作 */
function handleDelete(row) {
  //console.log(row);
  // const dictIds = [];
  // if (row.equipmentId != undefined) {
  //   dictIds.push(row.equipmentId);
  // } else {
  //   dictIds.push(...ids.value);
  // }
  PxMessageBox.confirm("是否确认删除？", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true
  })
    .then(async () => {
      const data = await handoverExceptionHandledelete({ id: row.id });
      PxMessage({
        message: "删除成功",
        type: "success"
      });
      getList();
    })
    .catch(() => {});
}

const { VITE_FANRUAN_BASE_URL, VITE_SERVE_API_STATIC_URL } = import.meta.env;
/** 导出按钮操作 */
function handleExport(row) {
  let Token = getToken().accessToken || "";
  let url = `${VITE_FANRUAN_BASE_URL}/webroot/decision/view/report?viewlet=water%252F%25E5%25BC%2582%25E5%25B8%25B8%25E6%258A%25A5%25E5%2591%258A.cpt&token=${Token}&id=${row.id}&format=excel&extype=simple&__filename__=${row.eventDescription}_${row.eventDate}&facCode=${localStorage.getItem("facCode") || ""}&apiHost=${VITE_SERVE_API_STATIC_URL}`;
  window.location.href = url;
  console.log(url);
}
</script>
<style scoped lang="scss">
.addEquip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99;
  width: calc(100% - 48px);
  height: calc(100% - 40px);
  padding-top: 10px;
  margin: 24px;
  background: white;
}

.editorEquip {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99;
  width: calc(100% - 48px);
  height: calc(100% - 40px);
  padding-top: 10px;
  margin: 24px;
  background: white;
}

.px-dialog-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 2002;
  width: 100%;
  height: 50px;
  padding-top: 10px;
  padding-right: 34px;
  text-align: right;
  background: white;
  box-shadow: 0 0 10px 0 rgb(0 0 0 / 16%);
}

.card-header {
  min-height: 40px;
  padding: 14px 15px 7px !important;
  cursor: pointer;
  user-select: none;
  background: #e7eefb;
}

.card-title {
  float: left;
  line-height: 14px;
}

.card-main {
  float: left;
  margin: -3px 8px 0 5px;
  cursor: pointer;
}

.box-card1 {
  width: calc(100% - 48px);
  margin: 24px;
  overflow: hidden;
  border: 1px solid #dfdfdf;
  border-radius: 4px;
}

.box-card2 {
  width: calc(100% - 48px);
  margin: 24px;
  overflow: hidden;
  border: 1px solid #dfdfdf;
  border-radius: 4px;
}

.box-card3 {
  width: calc(100% - 48px);
  margin: 24px;
  overflow: hidden;
  border: 1px solid #dfdfdf;
  border-radius: 4px;
}

.box-card4 {
  width: calc(100% - 48px);
  height: 261px;
  margin: 24px;
  overflow: hidden;
  border: 1px solid #dfdfdf;
  border-radius: 4px;
}

.demo-form-inline {
  .px-input {
    --px-input-width: calc(100% - 100px);
  }

  .px-form-item {
    width: 100%;
  }

  .px-textarea__inner {
    width: calc(100% - 100px) !important;
  }

  .px-form-item__label {
    font-weight: 400 !important;
  }
}

.form-div {
  padding: 24px;

  .px-row {
    margin-bottom: 16px;
  }
}

.px-icon {
  font-size: 16px !important;
}

:deep(.px-tabs__header) {
  // padding-left: 6px !important;
}

:deep(.px-tabs--top .px-tabs__item.is-top.is-active) {
  border: none;

  .pane-bottom {
    line-height: 30px;
    border-bottom: 2px solid #3b82f6;
  }
}

:deep(.px-tabs .px-tabs__item) {
  .pane-bottom {
    line-height: 30px;
  }
}

.pane-line {
  position: absolute;
  top: 12px;
  right: 0;
  height: 15px;
  color: #dcdcdc;
  border-left: 1px solid #dcdcdc;
}

:deep(.custom-form-ui .px-form-item) {
  width: 250px !important;
}

:deep(.px-upload) {
  --px-upload-dragger-padding-horizontal: 18px;
}

:deep(.px-upload--picture-card) {
  --px-upload-picture-card-size: 93px;

  position: absolute;
}

.file-upload-dom {
  :deep(.px-upload-list--picture-card .px-upload-list__item) {
    position: relative;
    width: 96px;
    height: 96px;
    overflow: visible;

    .filebg {
      position: absolute;
      display: none;
      width: 96px;
      height: 96px;
      background: #0006;
    }

    .filedelect {
      margin: 35px;
      font-size: 22px !important;
      color: white;
      cursor: pointer;
    }

    &:hover {
      .filebg {
        display: block;
      }
    }

    .filesvg {
      position: absolute;
      top: 30px;
      left: 30px;
      font-size: 70px;
    }
  }

  :deep(.px-upload--picture-card) {
    --px-upload-picture-card-size: 93px;

    border: none;
  }
}

:deep(.px-upload-list--picture-card .px-upload-list__item) {
  width: 96px;
  height: 96px;
}

.filename {
  position: absolute;
  bottom: -30px;
  width: 96px;
  overflow: hidden;
  color: rgb(166 167 172);
  text-overflow: ellipsis;
  white-space: nowrap;
}

:deep(.px-descriptions__label.px-descriptions__cell.is-bordered-label) {
  width: 166px;
  background: #e7eefb;
  border: 1px solid #dfdfdf;
}

:deep(.px-descriptions__body .px-descriptions__table.is-bordered .px-descriptions__cell) {
  border: 1px solid #dfdfdf;
}

.cell-item {
  text-align: right;
}

:deep(.px-table__header) {
  thead {
    tr {
      background-color: #e7eefb !important;

      th {
        font-size: 14px;
        font-weight: bold;
        color: #000;
        background-color: #e7eefb !important;
      }
      // th:nth-child(2) {
      //   font-size: 14px;
      //   font-weight: bold;
      //   color: #000;
      //   border-right: none;
      //   background-image: linear-gradient(to right, red, blue);
      // }
      // th:nth-child(3) {
      //   font-size: 14px;
      //   font-weight: bold;
      //   color: #000;
      //   border-right: none;
      //   background-image: linear-gradient(to right, #0400fc, yellow);
      // }
    }
  }
}

:deep(.px-table--isfixedHeader .px-table__header-wrapper .px-table-fixed-column--right) {
  background-color: #e7eefb !important;
}

:deep(.px-upload-list--picture-card > li:first-of-type) {
  margin-left: 104px;
}

:deep(.px-upload-list--picture-card) {
  min-height: 100px;
}
</style>
